package com.jd.sunny.util;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

import org.apache.log4j.Logger;

/**
 * @author :Sun Lin
 * @Description: 连接数据库工具类
 * @date :2012-9-12
 * @since:1.0.0
 */
public class DBConnectionUtil {

    public static void main(String[] args) {
        Connection connection = DBConnectionUtil.getConnection();
        DBConnectionUtil.closeConnection(connection);
    }

    private static Logger logger = LogUtil.getLogger();

    /**
     * 数据库配置文件
     */
    private static final String JDBC = "config_develop/db.properties";

    private static String driverClassName = "";
    private static String url = "";
    private static String username = "";
    private static String password = "";

    /**
     * 静态代码快
     */
    static {
        Properties properties = new Properties();
        try {
            //加载配置文件
             properties.load(DBConnectionUtil.class.getClassLoader().getResourceAsStream(JDBC));
//            properties.load(new BufferedInputStream(new FileInputStream(JDBC)));
            driverClassName = properties.getProperty("driverClassName").trim();
            url = properties.getProperty("url").trim();
            username = properties.getProperty("username");
            password = properties.getProperty("username");

            //1、加载驱动
            Class.forName(driverClassName);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }


    /**
     * 获取连接Connection
     *
     * @return
     */
    public static Connection getConnection() {
        Connection connection = null;
        try {
            connection = DriverManager.getConnection(url, username, password);
            if (connection != null) {
                logger.info("获取连接成功");
            }
        } catch (SQLException e) {
            logger.info("获取连接失败");
            e.printStackTrace();
        }
        return connection;
    }

    /**
     * 关闭Connection
     *
     * @param connection
     */
    public static void closeConnection(Connection connection) {
        if (connection != null) {
            try {
                connection.close();
                connection = null;
                logger.info("关闭连接成功");
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 关闭PreparedStatement
     *
     * @param preparedStatement
     */
    public static void closePreparedStatement(PreparedStatement preparedStatement) {
        if (preparedStatement != null) {
            try {
                preparedStatement.close();
                preparedStatement = null;
                logger.info("关闭PreparedStatement成功");
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 关闭ResultSet
     *
     * @param resultSet
     */
    public static void closeResultSet(ResultSet resultSet) {
        if (resultSet != null) {
            try {
                resultSet.close();
                resultSet = null;
                logger.info("关闭ResultSet成功");
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }


}
